﻿package component.fileupload.action;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import component.fileupload.exception.FileUploadException;
import component.fileupload.listener.IOutputStreamListener;
import component.fileupload.listener.UploadFileListener;
import component.fileupload.util.FileUtil;
import component.fileupload.vo.UploadFileVO;

import smart.*;

public class SmartFileUploadAction extends AbstractFileUploadAction {
	private SmartUpload mySmartUpload = new SmartUpload();
	
	public InputStream[] getInputStreams(ServletConfig config, HttpServletRequest request, HttpServletResponse response)
			throws FileUploadException {
		HttpSession session = request.getSession();
		
		fileInfoMap = new HashMap();
		// 创建UploadListener对象
		listener = new UploadFileListener(request.getContentLength());
		// 启动监听状态
		listener.start();
		// 将监听器对象的状态保存在Session中
		session.setAttribute("FILE_UPLOAD_STATS", listener.getState());
			
		try {
			mySmartUpload.initialize(config, request, response);
			mySmartUpload.setMaxFileSize(FileUtil.DEFAULT_SIZE_MAX);
			
			mySmartUpload.upload();
			
			SmartFiles files = mySmartUpload.getFiles();
			for (int i = 0; i < files.getCount(); i++) {
				SmartFile file = files.getFile(i);
				if (!file.isMissing()) {
					UploadFileVO vo = new UploadFileVO();
					
					vo.setFileName(file.getFileName());
					vo.setFilePath(file.getFilePathName());
					
					//vo.setIndex(fileInfoMap.size());
					vo.setIndex(Integer.parseInt(file.getFieldName().split("_")[1]));
					System.out.println("文件的顺序号：" + vo.getIndex());
					
					vo.setBinArray(file.getBinArray());
					vo.setStartData(file.getStartData());
					vo.setEndData(file.getEndData());
					vo.setFileSize(file.getSize());
					
					fileInfoMap.put(new Integer(fileInfoMap.size()), vo);
				}
			}
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			listener.error(e.getMessage());
			throw new FileUploadException(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			listener.error(e.getMessage());
			throw new FileUploadException(e);
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			listener.error(e.getMessage());
			throw new FileUploadException(e);
		}
		
		InputStream[] inArr = new InputStream[fileInfoMap.size()];
		for(int i = 0;i<fileInfoMap.size();i++){
			UploadFileVO vo = (UploadFileVO)fileInfoMap.get(new Integer(i));
			inArr[i] = vo.getInputStream();
		}
		return inArr;
	}
	
	public void destroy() {
		this.listener = null;
		
		//先销毁里面的数据
		for(int i = 0;i<fileInfoMap.size();i++){
			UploadFileVO vo = (UploadFileVO)fileInfoMap.get(new Integer(i));
			InputStream in = vo.getInputStream();
			try {
				if(in!=null) in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		fileInfoMap = null;
	}

}
